package com.vert.flowable.controller;

import com.alibaba.fastjson.JSONObject;
import com.vert.annotations.GetMapping;
import com.vert.annotations.Param;
import com.vert.annotations.RestController;
import com.vert.flowable.FlowableUtils;
import com.vert.utils.Results;
import io.vertx.core.http.HttpServerRequest;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.DeploymentBuilder;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceBuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Classname FlowableController
 * @Description TODO
 * @Date 2022/5/29 10:02
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/flowable")
public class FlowableController {

    @GetMapping(value = "/deploy",desc = "流程部署")
    public JSONObject processDeployController(@Param("name")String name){

       // String resuorce=this.getClass().getResource("/").getPath()+ File.separator+"processes"+File.separator+name+".bpmn20.xml";
     // System.out.println(resuorce);
      //  File file = new File(resuorce);
        File fileResource=new File("processes"+File.separator + File.separator+name+".bpmn20.xml");
    //  System.out.println(file.getPath());
        RepositoryService repositoryService=FlowableUtils.getRepositoryService();
      // 创建部署构建器
      DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
    // 添加资源
      deploymentBuilder.addClasspathResource(fileResource.getPath());
      // 执行部署
      deploymentBuilder.deploy();
      // 验证部署
      long count = repositoryService.createProcessDefinitionQuery().processDefinitionKey(name).count();
      if(count > 0){
        return Results.success("部署成功");
      }else {
        return Results.success("部署失败");
      }
    }

    @GetMapping(value = "/deployString",desc = "通过字符串部署")
    public JSONObject processDeploymentStringController(HttpServerRequest request){
        String text="";
      RepositoryService repositoryService=FlowableUtils.getRepositoryService();
      // 创建部署构建器
      DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
      // 执行部署
      deploymentBuilder.addString("single-task2.bpmn20.xml", text).deploy();
      // 验证部署
      long count = repositoryService.createProcessDefinitionQuery().processDefinitionKey("singleTask2").count();
      if(count > 0){
        return Results.success("部署成功");
      }else {
        return Results.success("部署失败");
      }
    }


    @GetMapping(value = "deploylist",desc = "查询部署的列表")
    public JSONObject deployDeploymentListControllers(){
        RepositoryService repositoryService=FlowableUtils.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        List<JSONObject> objectList=new ArrayList<>();
        for (ProcessDefinition processDefinition : list) {
            JSONObject object=new JSONObject();
            object.put("name",processDefinition.getName());
            object.put("id",processDefinition.getId());
            objectList.add(object);
        }
        return Results.success(objectList);
    }



    @GetMapping(value = "/create",desc = "创建任务")
    public JSONObject createTaskController(){
        String staffId = "1000";
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", staffId);

        RuntimeService runtimeService= FlowableUtils.getRuntimeService();
        ProcessInstanceBuilder processInstanceBuilder=runtimeService.createProcessInstanceBuilder();
        processInstanceBuilder.businessKey("123456789");
        processInstanceBuilder.processDefinitionKey("ask_for_leave");
        processInstanceBuilder.name("我要请假了");
        processInstanceBuilder.variables(map);
        ProcessInstance processInstance=processInstanceBuilder.start();

      //  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ask_for_leave", map);
        Authentication.setAuthenticatedUserId(null);
        runtimeService.setVariable(processInstance.getId(), "name", "javaboy");
        runtimeService.setVariable(processInstance.getId(), "reason", "休息一下");
        runtimeService.setVariable(processInstance.getId(), "days", 10);

        return Results.success("任务创建成功");

    }
}
